import { login, logout,getDict } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    dictList:[]
  }
}

const state = getDefaultState()

const mutations = {
 
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },

  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_permissions: (state, permissions) => {
    state.permissions = permissions
  },
  SET_DICT: (state, dictList) => {
    state.dictList = dictList
  }

}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
    
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        console.log(data);
        const {permissions,loginName,roles } = data.result
        localStorage.buttenpermissions = permissions
        localStorage.roles = roles
        localStorage.loginName = data.result.loginName;
        commit('SET_TOKEN', localStorage.token)
        commit('SET_NAME', loginName)
        setToken(localStorage.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      commit('SET_ROLES', localStorage.roles)
      let permissions =localStorage.buttenpermissions.split(',')
      commit('SET_permissions',permissions)
      resolve(permissions)
    })
  },
  getDict({ commit }) {
   
    return new Promise((resolve, reject) => {
    
      getDict().then(response => {
       commit('SET_DICT',response.data.result)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout().then(response => {
        sessionStorage.clear();
        localStorage.removeItem('token')
        localStorage.removeItem('buttenpermissions')
        localStorage.removeItem('roles')
        localStorage.removeItem('loginName')
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
      
    
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

